package gr.handmademarket.ui.panels;

import gr.handmademarket.bl.operations.RegisterBuyerOperation;
import gr.handmademarket.po.implementations.Buyer;
import gr.handmademarket.po.interfaces.User;
import gr.handmademarket.ui.WebHandMadeSession;
import gr.handmademarket.ui.modals.RegisterModal;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

/**
 * panel used on registration of a buyer.
 */
public class BuyerPanel 
extends Panel{

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * the local model.
	 */
	private CompoundPropertyModel<User> localModel;
	
	/**
	 * the form.
	 */
	private Form<User> form;
	
	/**
	 * the username field.
	 */
	@SuppressWarnings("nls")
	private TextField<String> usernameField = new RequiredTextField<String>("username");
	
	/**
	 * the username field.
	 */
	@SuppressWarnings("nls")
	private TextField<String> passwordField = new RequiredTextField<String>("password");
	
	/**
	 * the first name field.
	 */
	@SuppressWarnings("nls")
	private TextField<String> firstNameField = new RequiredTextField<String>("firstName");
	
	/**
	 * the last name field.
	 */
	@SuppressWarnings("nls")
	private TextField<String> lastNameField = new RequiredTextField<String>("lastName");
	/**
	 * submit link.
	 */
	@SuppressWarnings("nls")
	private AjaxButton submit = new AjaxButton("submitBtn", form) {

		/**
		 * serialVersionUID.
		 */
		private static final long serialVersionUID = 1L;

		@Override
		protected void onSubmit(AjaxRequestTarget target, Form<?> form1) {
			RegisterBuyerOperation op = new RegisterBuyerOperation();
			if (localModel.getObject() instanceof Buyer){
				op.setBuyer((Buyer) localModel.getObject());
			}
			try{
				op.execute();
				WebHandMadeSession session = (WebHandMadeSession) WebHandMadeSession.get();
				session.setUser(op.getBuyer());
				RegisterModal modal = (RegisterModal) BuyerPanel.this.getParent().getParent();
				modal.close(target);
			} catch (Exception e) {
				error(e.getMessage());
			}
		}
		
		@Override
		protected void onError(AjaxRequestTarget target, Form<?> form1) {
			target.add(BuyerPanel.this.getParent().get("feedbackPanel"));
		}
		
	};

	
	/**
	 * Creates a new UserPanel object. 
	 *
	 * @param id
	 * @param model
	 */
	@SuppressWarnings("nls")
	public BuyerPanel(String id, IModel<User> model) {
		super(id, model);
		localModel = (CompoundPropertyModel<User>) model;
		form = new Form<User>("form", localModel);
		form.add(usernameField,passwordField,firstNameField,lastNameField,submit);
		add(form);
	}

}
